const GitServer = require('./GitServer')
const GiteeRequest = require('./GiteeRequest')

class Gitee extends GitServer {
  constructor() {
    super('gitee')
    this.request = null
  }

  setToken(token) {
    super.setToken(token)
    this.request = new GiteeRequest(token)
  }

  getUser() {
    return this.request.get('/user')
  }

  getOrg(username) {
    return this.request.get(`/users/${username}/orgs`, {
      page: 1,
      per_page: 100,
    })
  }

  getRepo(login, name) {
    return this.request.get(`/repos/${login}/${name}`).then(res => {
      if (res && res.status && res.status !== 200 && !res.owner) {
        // 暂时用owner字段去增强判断远程仓库是否存在
        return null
      }
      return res
    })
  }

  createRepo(name) {
    // https://gitee.com/api/v5/user/repos
    return this.request.post('/user/repos', {
      name,
    })
  }

  createOrgRepo(name, login) {
    console.log(name, login)
    return this.request.post(`/orgs/${login}/repos`, {
      name,
    })
  }

  getRemote(login, name) {
    return `git@gitee.com:${login}/${name}.git`
  }

  getTokenHelp() {
    return 'https://gitee.com/help/articles/4191'
  }
  getTokenUrl() {
    return 'https://gitee.com/personal_access_tokens'
  }
}

module.exports = Gitee
